package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import csu.web.mypetstore.mapper.OrderDetailMapper;
import csu.web.mypetstore.mapper.OrdersMapper;
import csu.web.mypetstore.pojo.DeliveryOrder;
import csu.web.mypetstore.pojo.OrderDetail;
import csu.web.mypetstore.pojo.Orders;
import csu.web.mypetstore.pojo.vo.ItemInfo;
import csu.web.mypetstore.service.*;
import csu.web.mypetstore.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 18632
 * @description 针对表【orders】的数据库操作Service实现
 * @createDate 2025-03-10 17:03:47
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService{
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CartService cartService;

    private static final ObjectMapper objectMapper = new ObjectMapper();


    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private AccountService accountService;

    @Autowired
    private DeliveryOrderService deliveryOrderService;
    @Autowired
    private ProductService productService;

    @Override
    public List<Orders> queryAllOrders() {
        return ordersMapper.selectList(null);
    }

    @Override
    public int insertOrder(Orders orders) {



       int orderId=ordersMapper.insert(orders);
       return orderId;
    }



    @Override
    public void updateOrder(Orders order) {
        Wrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>().eq(Orders::getOrderid, order.getOrderid());

        ordersMapper.update(order, wrapper);

    }

    @Override
    public void deleteOrder(int orderId) {
        ordersMapper.deleteById(orderId);
        //orderDetailMapper.delete(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderid, orderId));

    }
    @Override
    public List<Orders> getOrdersByUsername(int userId) {
        return ordersMapper.selectList(
                new LambdaQueryWrapper<Orders>()
                        .eq(Orders::getUserid, userId) // 查询条件：username
        );
    }
    @Override
    public Orders getOrder(int orderId) {

        Wrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>().eq(Orders::getOrderid, orderId);

        return ordersMapper.selectOne(wrapper);
    }
    @Override
    public void deleteOrderDetail(int orderId) {
        orderDetailMapper.delete(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderid, orderId));
    }
    @Override
    public Result addNewOrder(String username,Orders orders) {
        int userId = accountService.getUserId(username);
        orders.setUserid(userId);
        orders.setTotalprice(cartService.getTotal(userId));
         insertOrder(orders);
         cartService.deleteAllItem(username);
         return Result.success("添加新订单成功");


    }
    @Override
    public Result getPackage(String username) {
        List<DeliveryOrder> ordersList = deliveryOrderService.getOrderByUsername(username);
        Map res=new HashMap();//一个orderid对应一个包裹信息
        for(DeliveryOrder orders:ordersList){
            List<ItemInfo> itemInfo=new ArrayList<>();
           List<OrderDetail> orderDetails=orderDetailMapper.selectList(new LambdaQueryWrapper<OrderDetail>()
                    .eq(OrderDetail::getOrderid,orders.getOrderid()));
            for(OrderDetail orderDetail:orderDetails){
                ItemInfo item=new ItemInfo();
                item.setItemId(orderDetail.getItemid());
                item.setQuantity(orderDetail.getQuantity());
                String image = productService.getProductByItemId(orderDetail.getItemid()).getImage();
                item.setImage(image);
                itemInfo.add(item);
            }
          res.put(orders.getOrderid(),itemInfo);
        }
        //System.out.println(res);
       try{
          // System.out.println(objectMapper.writeValueAsString(res));
       }catch (Exception e){
           e.printStackTrace();
       }

        return Result.success(res);
    }
}




